﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.Koc.Models;
using System.Collections;

namespace CSharp.Libs.Koc
{
    public class SeekKnight : IDisposable
    {
        #region declarations
        private Kernel _KRNL = null;

        private ArrayList _aryKocActive = new ArrayList();
        private ArrayList _aryKocInactive = new ArrayList();

        #region events
        public delegate void BeginSeekKnightsEventHandler(object sender);
        public event BeginSeekKnightsEventHandler BeginSeekKnights;
        private void ThrowBeginSeekKnightsEvent()
        {
            if(BeginSeekKnights != null)
            {
                BeginSeekKnights(this);
            }
        }

        public delegate void KnightSeekedEventHandler(object sender, SeekedKnightEventArgs skea);
        public event KnightSeekedEventHandler KnightSeeked;
        private void ThrowKnightSeekedEvent(
            SeekKnightItem ski)
        {
            if(KnightSeeked != null)
            {
                KnightSeeked(this, new SeekedKnightEventArgs(ski));
            }
        }

        public delegate void EndSeekKnightsEventHandler(object sender);
        public event EndSeekKnightsEventHandler EndSeekKnights;
        private void ThrowEndSeekKnightsEvent()
        {
            if(EndSeekKnights != null)
            {
                EndSeekKnights(this);
            }
        }
        #endregion
        #endregion



        #region constructor / finalizer
        public SeekKnight(
            Kernel KRNL)
        {
            _KRNL = KRNL;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(disposing)
            {
                // get rid of managed resources         
                _KRNL = null;
                _aryKocActive = null;
                _aryKocInactive = null;
            }
            // get rid of unmanaged resources     
        }

        ~SeekKnight()
        {
            Dispose(false);
        }
        #endregion



        #region event handling
        #endregion



        #region private functions
        /// <summary>seeks a list with FB friends that are not your knights yet.</summary>
        /// <param name="PID">?propably ProvinceID?</param>
        /// <history>created  26.02.2012</history>
        private void _seekKnights(
            int PID,
            int ServerId,
            string FbUid)
        {
            try
            {
                //check if PhpCommandGenarator was initialized
                if(PhpCommandGenerator.IsInitialized)
                {
                    //Throw start
                    ThrowBeginSeekKnightsEvent();

                    //create PhpCommand seekknight
                    using(PhpCommand pcSeekKnight = PhpCommandGenerator.GetPhpCommand(_KRNL.EC, PhpCommands.seekknight, ServerId, FbUid))
                    {
                        //post it and get its response
                        Hashtable htMainResponse=null;
                        pcSeekKnight.Parameters[PhpCommandParameters.PID] = PID.ToString();
                        pcSeekKnight.GetAnswerHashtable(out htMainResponse);

                        //check if response is ok
                        if(_KRNL.ReturnHashtableOk(htMainResponse))
                        {
                            //get Data ArrayList
                            ArrayList aryData = htMainResponse[constants.KEY_DATA] as ArrayList;
                            if(aryData != null && aryData.Count > 0)
                            {
                                //get Koc active FB friends ArrayList
                                ArrayList aryActive = aryData[0] as ArrayList;
                                if(aryActive != null && aryActive.Count > 0)
                                {
                                    _aryKocActive.Clear();

                                    foreach(Hashtable htActive in aryActive)
                                    {
                                        SeekKnightItem SKI = new SeekKnightItem(_KRNL, htActive);
                                        _aryKocActive.Add(SKI);

                                        //Throw seeked
                                        ThrowKnightSeekedEvent(SKI);
                                    }
                                }

                                //get Koc inactive FB friends ArrayList
                                ArrayList aryInactive = aryData[1] as ArrayList;
                                if(aryInactive != null && aryInactive.Count > 0)
                                {
                                    _aryKocInactive.Clear();

                                    foreach(Hashtable htInactive in aryInactive)
                                    {
                                        SeekKnightItem SKI = new SeekKnightItem(_KRNL, htInactive);
                                        _aryKocInactive.Add(SKI);

                                        //Throw seeked
                                        ThrowKnightSeekedEvent(SKI);
                                    }
                                }
                            }
                        }
                    }

                    //Throw End
                    ThrowEndSeekKnightsEvent();
                }
            }
            catch(Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion



        #region interface
        #region properties
        public ArrayList KocActiveFbFriends { get { return _aryKocActive; } }
        public ArrayList KocInactiveFbFriends { get { return _aryKocInactive; } }
        #endregion

        #region methods
        /// <summary>seeks a list with FB friends that are not your knights yet.</summary>
        /// <param name="PID">?propably ProvinceID?</param>
        /// <history>created  26.02.2012</history>
        public void SeekKnights(
            int PID,
            int ServerId,
            string FbUid)
        {
            _seekKnights(PID, ServerId, FbUid);
        }
        #endregion
        #endregion
    }
}
